<template>
  <div class="wrap-item">
    <!-- 搜索-->
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-input
          v-model.trim="formSearch.param.name"
          placeholder="请输模板名称"
          maxlength="20"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.param.status"
          placeholder="请选择状态"
        >
          <el-option label="启用" value="0"></el-option>
          <el-option label="停用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span>
        <span
          class="wrap-btn wrap-success"
          v-if="$checkPermission(101)"
          @click="add"
          >添加应用</span
        >
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-item-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
      >
        <el-table-column
          type="index"
          width="50"
          label="序号"
          fixed
          align="center"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="应用名称"
          width="200px"
          fixed
        ></el-table-column>
        <el-table-column label="是否接入" width="80px">
          <template slot-scope="scope">
            <span v-if="scope.row.acceptFlag == 0">是</span>
            <span v-if="scope.row.acceptFlag == 1">否</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="mainUrl"
          label="PC端地址"
          width="300px"
        ></el-table-column>
        <el-table-column
          prop="contactPeo"
          label="负责人"
          width="130px"
        ></el-table-column>
        <el-table-column
          prop="contactTel"
          label="负责人电话"
          width="120px"
        ></el-table-column>
        <el-table-column
          prop="customerName"
          label="所属客户"
          width="200px"
        ></el-table-column>
        <el-table-column prop="tradeType" label="可见范围" width="500px">
          <template slot-scope="scope">
            <span v-for="(item, index) in scope.row.scope" :key="index">
              {{
                item.userName + (scope.row.scope.length == index + 1 ? '' : ',')
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="addTime"
          label="创建时间"
          width="160px"
        ></el-table-column>
        <el-table-column label="状态" width="80px" align="center" fixed="right">
          <template slot-scope="scope">
            <el-link
              type="success"
              :underline="false"
              v-if="scope.row.status == 0"
              >启用</el-link
            >
            <el-link
              type="danger"
              :underline="false"
              v-if="scope.row.status == 1"
              >停用</el-link
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="80px"
          fixed="right"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="view" v-if="$checkPermission(104)"
                  >查看</el-dropdown-item
                >
                <el-dropdown-item command="edit" v-if="$checkPermission(102)"
                  >编辑</el-dropdown-item
                >
                <el-dropdown-item
                  command="changeStatus"
                  v-if="scope.row.status == 1 && $checkPermission(105)"
                  >启用</el-dropdown-item
                >
                <el-dropdown-item
                  command="changeStatus"
                  v-if="scope.row.status == 0 && $checkPermission(106)"
                  >停用</el-dropdown-item
                >
                <el-dropdown-item command="remove" v-if="$checkPermission(103)"
                  >删除</el-dropdown-item
                >
                <el-dropdown-item command="reset" v-if="$checkPermission(107)"
                  >重置密码</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <web-page
      :page="formSearch.pageNum"
      :size="formSearch.pageSize"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    ></web-page>
    <!-- 对话框 -->
    <el-drawer :title="title" :wrapperClosable="false" :visible.sync="visible">
      <el-form label-width="100px" :model="form" size="medium">
        <el-form-item label="应用名称" required>
          <el-input
            v-model.trim="form.name"
            clearable
            maxlength="20"
            placeholder="请输入应用名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="PC端地址">
          <el-input
            v-model.trim="form.mainUrl"
            clearable
            maxlength="200"
            placeholder="请输入PC端地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="移动端地址">
          <el-input
            v-model.trim="form.mobileUrl"
            clearable
            maxlength="200"
            placeholder="请输入移动端地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="获取菜单地址">
          <el-input
            v-model.trim="form.menuUrl"
            clearable
            maxlength="200"
            placeholder="请输入获取菜单地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="事件订阅地址">
          <el-input
            v-model.trim="form.notifyUrl"
            clearable
            maxlength="200"
            placeholder="请输入事件订阅地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="事件订阅类型">
          <el-select
            clearable
            multiple
            v-model="form.notifyEventTypeArr"
            placeholder="请选择事件订阅类型"
            style="width: 100%"
          >
            <el-option
              v-for="item in notifyEventType"
              :key="item.dicValue"
              :label="item.dicLabel"
              :value="item.dicValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="负责人" required>
          <el-input
            v-model.trim="form.contactPeo"
            clearable
            maxlength="10"
            placeholder="请输入负责人"
          ></el-input>
        </el-form-item>
        <el-form-item label="负责人电话" required>
          <el-input
            v-model.trim="form.contactTel"
            clearable
            maxlength="11"
            placeholder="请输入负责人电话"
          ></el-input>
        </el-form-item>
        <el-form-item label="可见角色">
          <el-select
            clearable
            multiple
            filterable
            v-model="form.roleScope"
            value-key="userId"
            placeholder="请选择可见角色"
            style="width: 100%"
          >
            <el-option
              v-for="item in roleData"
              :key="item.userId"
              :label="item.userName"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="可见人员">
          <el-input
            type="textarea"
            v-model.trim="form.scopeName"
            placeholder="请选择可见人员"
            resize="none"
            @focus="panelVisible = true"
          ></el-input>
        </el-form-item>
        <el-form-item label="应用图标" required>
          <web-upload @upload="upload" accept="image/*" :size="[75, 75]" />
          <img
            v-if="form.imgUrl"
            :src="form.imgUrl"
            style="position: absolute; top: 0; left: 100px; max-height: 82px"
          />
        </el-form-item>
        <el-form-item label="是否接入">
          <el-select v-model="form.acceptFlag" style="width: 100%">
            <el-option label="是" value="0"></el-option>
            <el-option label="否" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入驻类型">
          <el-select
            clearable
            multiple
            v-model="form.userItemType"
            placeholder="请选择入驻类型"
            style="width: 100%"
          >
            <el-option
              v-for="item in userItemType"
              :key="item.dicValue"
              :label="item.dicLabel"
              :value="item.dicValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="令牌验证方式">
          <el-select v-model="form.tokenValidate" style="width: 100%">
            <el-option label="cookie" value="1"></el-option>
            <el-option label="临时code" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="应用描述">
          <el-input
            type="textarea"
            v-model.trim="form.appDesc"
            placeholder="请输入应用描述"
            maxlength="200"
            resize="none"
          ></el-input>
        </el-form-item>
        <el-form-item style="text-align: end">
          <span class="wrap-btn wrap-success" @click="save">确定</span>
          <span class="wrap-btn wrap-default" @click="visible = false"
            >取消</span
          >
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 查看 -->
    <el-dialog title="查看" :visible.sync="isLook" width="888px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="应用名称">
          {{ lockData.name }}
        </el-descriptions-item>
        <el-descriptions-item label="应用状态">
          {{ lockData.status == 0 ? '启用' : '禁用' }}
        </el-descriptions-item>
        <el-descriptions-item label="PC端地址" :span="2">
          {{ lockData.mainUrl }}
        </el-descriptions-item>
        <el-descriptions-item label="移动端地址" :span="2">
          {{ lockData.mobileUrl }}
        </el-descriptions-item>
        <el-descriptions-item label="获取菜单地址" :span="2">
          {{ lockData.menuUrl }}
        </el-descriptions-item>
        <el-descriptions-item label="事件订阅地址" :span="2">
          {{ lockData.notifyUrl }}
        </el-descriptions-item>
        <el-descriptions-item label="事件订阅类型" :span="2">
          {{ lockData.notifyEventTypeTxt }}
        </el-descriptions-item>
        <el-descriptions-item label="可见角色" :span="2">
          <i v-for="(item, index) in lockData.roleScope" :key="index">
            {{
              item.userName +
              (lockData.roleScope.length == index + 1 ? '' : ',')
            }}
          </i>
        </el-descriptions-item>
        <el-descriptions-item label="可见人员" :span="2">
          <i v-for="(item, index) in lockData.scope" :key="index">
            {{
              item.userName + (lockData.scope.length == index + 1 ? '' : ',')
            }}
          </i>
        </el-descriptions-item>
        <el-descriptions-item label="负责人">
          {{ lockData.contactTel }}
        </el-descriptions-item>
        <el-descriptions-item label="应用图标">
          <img
            v-if="lockData.imgUrl"
            :src="lockData.imgUrl"
            style="max-height: 60px"
          />
        </el-descriptions-item>
        <el-descriptions-item label="是否接入">
          {{ lockData.acceptFlag == 0 ? '是' : '否' }}
        </el-descriptions-item>
        <el-descriptions-item label="入驻类型">
          {{ lockData.settleTypeTxt }}
        </el-descriptions-item>
        <el-descriptions-item label="令牌验证方式">
          {{ lockData.tokenValidate == 1 ? 'cookie' : '临时code' }}
        </el-descriptions-item>
        <el-descriptions-item label="应用描述" :span="2">
          <el-tooltip
            popper-class="ellipsis-tip-span2"
            :content="lockData.appDesc"
            placement="top-start"
          >
            <span class="ellipsis-span2">{{ lockData.appDesc }}</span>
          </el-tooltip>
        </el-descriptions-item>
      </el-descriptions> </el-dialog
    ><!-- 用户面板 -->
    <user-panel-plus
      :visible="panelVisible"
      :data="user"
      :check="checkUser"
      :defaultProps="defaultProp"
      @close="panelVisible = false"
      @save="saveUser"
    />
  </div>
</template>

<script>
import { isPhone, isUrl, isIp } from '@u/validate'
export default {
  data() {
    return {
      fileDomain: JSON.parse(sessionStorage.userInfo).fileDomain,
      roleData: [],
      user: [],
      checkUser: [],
      defaultProp: {
        key: 'id', //唯一id string
        defaultParentId: '0', //默认父级id string
        parentId: 'parentId', //父级id string
        id: 'userId', //用户id string
        name: 'name', //名称 string
        children: 'children', //子集容器
      },
      notifyEventType: [],
      userItemType: [],
      formSearch: {
        pageNum: 1,
        pageSize: 10,
        param: {},
      },
      total: null,
      tableData: [],
      visible: false,
      title: '添加应用',
      panelVisible: false,
      form: {},
      isLook: false,
      lockData: {},
    }
  },
  created() {
    this.roleList()
    this.userList()
    this.notifyEventTypeList()
    this.userItemTypeList()
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表
      this.$axios.post('/admin/app/page', this.formSearch).then((data) => {
        this.tableData = []
        if (data.code == 200) {
          this.tableData = data.data.list
          this.total = data.data.total
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    sizeChange(val) {
      //每页条数
      this.formSearch.pageSize = val
      this.itemList()
    },
    currentChange(val) {
      //分页
      this.formSearch.pageNum = val
      this.itemList()
    },
    search() {
      //搜索
      this.formSearch.pageNum = 1
      this.itemList()
    },
    add() {
      //添加
      this.checkUser = []
      this.title = '添加应用'
      this.form = {
        acceptFlag: '1',
        tokenValidate: '1',
        status: '0',
        scope: [],
        notifyEventTypeArr: [],
        userItemType: [],
      }
      this.visible = true
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'view':
          //查看
          this.$axios.get('/admin/app/find?id=' + row.id).then((data) => {
            this.lockData = {}
            if (data.code == 200) {
              this.lockData = data.data
              this.isLook = true
            } else {
              this.$message.error(data.msg)
            }
          })
          break
        case 'edit':
          this.title = '编辑应用'
          this.$axios.get('/admin/app/find?id=' + row.id).then((data) => {
            this.form = {
              acceptFlag: '1',
              tokenValidate: '1',
              status: '0',
              scope: [],
              notifyEventTypeArr: [],
              userItemType: [],
            }
            if (data.code == 200) {
              this.form = data.data
              this.form.status = String(this.form.status)
              this.$set(
                this.form,
                'notifyEventTypeArr',
                !data.data.notifyEventType
                  ? []
                  : data.data.notifyEventType.split(',')
              )
              this.$set(
                this.form,
                'userItemType',
                !data.data.settleType ? [] : data.data.settleType.split(',')
              )
              const arr = []
              this.checkUser = []
              this.form.scope.forEach((element) => {
                arr.push(element.userName)
                this.checkUser.push({
                  id: '',
                  userId: element.userId,
                  name: element.userName,
                })
              })
              this.form.scopeName = arr.join(',')
              this.visible = true
            } else {
              this.$message.error(data.msg)
            }
          })
          break
        case 'changeStatus':
          //启用停用
          const status = row.status == 0 ? 1 : 0
          this.$axios
            .get('/admin/app/start?id=' + row.id + '&status=' + status)
            .then((data) => {
              if (data.code == 200) {
                this.itemList()
                this.$message.success('操作成功！')
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'remove':
          //删除
          this.$confirm('您确定要删除该条数据吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              this.$axios.get('/admin/app/del?id=' + row.id).then((data) => {
                if (data.code == 200) {
                  this.itemList()
                  this.$message.success('操作成功！')
                } else {
                  this.$message.error(data.msg)
                }
              })
            })
            .catch(() => {})
          break
      }
    },
    save() {
      //保存
      if (!this.form.name) {
        this.$message.error('请输入应用名称')
        return
      }
      // if (!this.form.mainUrl) {
      //   this.$message.error('请输入正确的PC端地址')
      //   return
      // }
      // if (!isUrl(this.form.mainUrl) && !isIp(this.form.mainUrl)) {
      //   this.$message.error('请输入正确的PC端地址')
      //   return
      // }
      // if (this.form.mobileUrl && !isUrl(this.form.mobileUrl) && !isIp(this.form.mobileUrl)) {
      //   this.$message.error('请输入正确的移动端地址')
      //   return
      // }
      // if (this.form.menuUrl && !isUrl(this.form.menuUrl)&& !isIp(this.form.menuUrl)) {
      //   this.$message.error('请输入正确的取菜单地址')
      //   return
      // }
      // if (this.form.notifyUrl && !isUrl(this.form.notifyUrl)&& !isIp(this.form.notifyUrl)) {
      //   this.$message.error('请输入正确的事件订阅地址')
      //   return
      // }
      if (!this.form.contactPeo) {
        this.$message.error('请输入负责人')
        return
      }
      if (!isPhone(this.form.contactTel)) {
        this.$message.error('请输入正确的负责人电话')
        return
      }
      if (!this.form.imgUrl) {
        this.$message.error('请上传应用图标')
        return
      }
      if (this.form.notifyEventTypeArr?.length) {
        this.form.notifyEventType = this.form.notifyEventTypeArr.join(',')
      } else {
        this.form.notifyEventType = ''
      }
      if (this.form.userItemType?.length) {
        this.form.settleType = this.form.userItemType.join(',')
      } else {
        this.form.settleType = ''
      }
      this.$axios.post('/admin/app/edit', this.form).then((data) => {
        if (data.code == 200) {
          this.visible = false
          this.$message.success('操作成功！')
          this.itemList()
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    upload(data) {
      //上传
      this.$set(this.form, 'imgUrl', data.fileDomain + data.url)
    },
    saveUser(data) {
      //保存用户
      const arr = []
      this.form.scope = []
      data.forEach((element) => {
        if (element.userId != 0) {
          arr.push(element.name)
          this.form.scope.push({
            type: 1,
            userId: element.userId,
            userName: element.name,
          })
        }
      })
      this.form.scopeName = arr.join(',')
      this.panelVisible = false
    },
    roleList() {
      // 获取角色
      this.$axios.get('/admin/role/listRole').then((data) => {
        this.roleData = []
        if (data.code == 200) {
          data.data.forEach((element) => {
            this.roleData.push({
              type: 2,
              userId: element.id,
              userName: element.roleName,
            })
          })
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    userList() {
      //获取部门树及部门下的人员
      this.$axios.get('/admin/org/listUnitTreeV2').then((data) => {
        this.user = []
        if (data.code == 200) {
          this.user = data.data
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    notifyEventTypeList() {
      // 获取事件订阅类型
      this.$axios
        .get('/admin/dic/listDic?type=notifyEventType')
        .then((data) => {
          this.notifyEventType = []
          if (data.code == 200) {
            this.notifyEventType = data.data
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    userItemTypeList() {
      // 获取入驻类型
      this.$axios.get('/admin/dic/listDic?type=userItemType').then((data) => {
        this.userItemType = []
        if (data.code == 200) {
          this.userItemType = data.data
        } else {
          this.$message.error(data.msg)
        }
      })
    },
  },
}
</script>
